package net.JwtDemo.controller;


import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.JwtDemo.dto.JsonResult;
import net.JwtDemo.dto.LoginUser;
import net.JwtDemo.dto.RequestUser;
import net.JwtDemo.entity.UserInfo;
import net.JwtDemo.service.AuthService;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;

@RestController
@RequestMapping("/api/v1/auth")
@RequiredArgsConstructor
@Slf4j
public class AuthController {
    private final AuthService authService;

    /*
    @PostMapping("/register")
    public ResponseEntity<JsonResult> register(@RequestBody LoginUser request) {
        return ResponseEntity.ok(authService.register(request));
    }
    */

    @PostMapping("/getToken")
    public JsonResult getToken(@RequestBody RequestUser request) {
    	try {
    		ObjectMapper mapper = new ObjectMapper();
    		String json = mapper.writeValueAsString(request);
    		log.debug(json);
    	} catch (Exception e) {
			log.error("无法将请求参数转为LoginUser对象");
			return new JsonResult("1", "无法将请求参数转为LoginUser对象");
		}
    	
    	return authService.authenticate(new LoginUser(request));
    }
}